package com.service.share.impl;

import com.alibaba.fastjson.JSON;
import com.dao.share.ChatMapper;
import com.entity.Answer;
import com.entity.share.chat.Chat;
import com.entity.share.chat.ChatTable;
import com.service.share.ChatService;
import com.util.Util;
import net.sf.json.JSONArray;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ChatServiceImpl implements ChatService {

    @Autowired
    private ChatMapper chatMapper;

    //获取项目的绝对路径
    private String path = System.getProperty("user.dir") + "/static/";
    @Value("${ststics.img.chatImg}")
    private String chatImg;


    public Object addChatRecord(Chat chat, List<MultipartFile> files) {

        Answer answer = new Answer();
        try {
            //判断是否是img类型
            if (chat.getType().equals("img")) {
                //上传图片的路径
                answer = (Answer) Util.uploadFiles(files, path + chatImg + "/" + chat.getAdvisoryID() + "/");
                if (answer != null) {
                    return answer;
                }
                //将content修改成图片的路径
                chat.setContent(chatImg + "/" + chat.getAdvisoryID() + "/" + chat.getContent());
            }
            if (chatMapper.addChat(chat) != 1) {
                answer = new Answer();
                answer.setCode(0);
                answer.setError("聊天发送失败！");
                return answer;
            }
        } catch (Exception e) {
            e.printStackTrace();
            answer = new Answer();
            answer.setCode(0);
            answer.setError("聊天发送失败！");
            return answer;
        }
        answer = new Answer();
        answer.setCode(1);
        return answer;
    }

    public Object queryChat(int advisoryID, int userID) {
        Answer answer = new Answer();
        try {
            //查询所有的聊天记了
            List<Chat> chars = chatMapper.selectChatByPatientID(advisoryID,userID);
            //遍历判断是否是自己发送的聊天
            for (Chat chat : chars) {
                if (chat.getUserID().equals(userID)) {
                    chat.setIsSelf(true);
                } else {
                    chat.setIsSelf(false);
                }
            }
            answer.setCode(1);
            answer.setData(chars);
            return answer;
        } catch (Exception e) {
            e.printStackTrace();
            answer.setCode(0);
            answer.setError("发生错误！");
            return answer;
        }

    }


    public Object viewExcel(String excelPath) {
        Answer answer = new Answer();
        Workbook wb;
        Sheet sheet;
        Row row;
        String[] name = {"inner", "panoramic", "head", "deformity1", "deformity2", "deformity3", "deformity4", "data", "existing", "diagnose"};
        String filepath = "f:/MakeModel.xlsx";
        List<Map<String, List<List<String>>>> data = new ArrayList<>();
        if (filepath == null) {
            answer.setCode(0);
            answer.setError("文件为空！");
            return answer;
        }
        String ext = filepath.substring(filepath.lastIndexOf("."));
        try {
            InputStream is = new FileInputStream(filepath);
            if (".xls".equals(ext)) {
                wb = new HSSFWorkbook(is);
            } else if (".xlsx".equals(ext)) {
                wb = new XSSFWorkbook(is);
            } else {
                wb = null;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            answer.setCode(0);
            answer.setError("发生错误");
            return answer;

        } catch (IOException e) {
            e.printStackTrace();
            answer.setCode(0);
            answer.setError("发生错误");
            return answer;
        }
        if (wb == null) {
            answer.setCode(0);
            answer.setError("发生错误");
            return answer;
        }
        int sheetnum = wb.getNumberOfSheets();
        System.out.println(sheetnum);
        for (int j = 0; j < sheetnum; j++) {
            sheet = wb.getSheetAt(j);
            row = sheet.getRow(0);
//        // 总列数
            int colNum = row.getPhysicalNumberOfCells();
            // 得到总行数
            int rowNum = sheet.getLastRowNum();
            Map<String, List<List<String>>> map = new HashMap<>();
            List<List<String>> lists = new ArrayList<>();
            for (int i = 0; i <= rowNum; i++) {
                List<String> list = new ArrayList<>();
                row = sheet.getRow(i);
                for (int k = 0; k < colNum; k++) {
                    //System.out.println(row.getCell(k));
                    list.add(String.valueOf(row.getCell(k)));
                }
                lists.add(list);
            }
            map.put(name[j], lists);
            data.add(map);
        }
        JSONArray array = (JSONArray) JSON.toJSON(data);
        System.out.println(array);
        answer.setCode(1);
        return answer;
    }

    public Object updateChatTables(String type, Integer ID){
        Answer answer = new Answer();
        try {
            List<ChatTable> chatTables = chatMapper.updateChatTables(type,ID);
            answer.setCode(1);
            answer.setData(chatTables);
        }catch (Exception e){
            e.printStackTrace();
            answer.setCode(0);
            return answer;
        }
        return answer;
    }
}
